package com.md.infrastructure.persistence.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.md.base.BaseRepositoryImpl;
import com.md.domain.model.po.RolePO;
import com.md.domain.repository.RoleRepository;
import com.md.infrastructure.persistence.mapper.RoleMapper;
import com.md.util.ObjectUtil;
import com.md.value.RoleStatusEnum;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author 刘骄阳
 * 2022-06-12 1:22
 */
@Repository
public class DAORoleRepository extends BaseRepositoryImpl<RolePO, RoleMapper> implements RoleRepository {
  public DAORoleRepository(final RoleMapper mapper) {
    super(mapper);
  }

  @Override
  public RolePO queryRoleByName(final String name) {
    return mapper.selectOne(new LambdaQueryWrapper<RolePO>().eq(RolePO::getName, name));
  }

  @Override
  public List<RolePO> selectChildrenRoleList(final String roleLink) {
    return mapper.selectChildrenRoleList(roleLink);
  }

  @Override
  public Page<RolePO> selectListByPage(final Long pageNum,
                                       final Long pageSize,
                                       final String order,
                                       final String name,
                                       final Integer status) {
    QueryWrapper<RolePO> wrapper = new QueryWrapper<>();
    if (ObjectUtil.notEmpty(order)) {
      wrapper.orderByAsc(order);
    }
    wrapper.lambda().likeRight((ObjectUtil.notEmpty(name)), RolePO::getName, name)
      .eq(ObjectUtil.notEmpty(status), RolePO::getStatus, status);
    return mapper.selectPage(Page.of(pageNum, pageSize), wrapper);
  }

  /**
   * 更新角色状态
   *
   * @param roleId 角色id
   * @param status 状态
   */
  @Override
  public void updateRoleStatus(final Long roleId, final RoleStatusEnum status) {
    RolePO updatePO = new RolePO();
    updatePO.setId(roleId);
    updatePO.setStatus(status.getCode());
    mapper.updateById(updatePO);
  }
}
